package gui;

import java.awt.Font;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class GraficoPizza2d  extends ApplicationFrame {

	public GraficoPizza2d(String titulo, String[] v, double[] va){
		super(titulo);
		setContentPane(criaPainel(v,va));
	}
	
	private static PieDataset PizzaSetDados(String[] elementos, double[]valores){
		DefaultPieDataset dataset = new DefaultPieDataset();
		int x = 0;
		while(x<elementos.length){
			dataset.setValue(elementos[x], valores[x]);
			x++;
		}
		return dataset;
	}
	
	 private static JFreeChart criaChart(PieDataset dataset) {
         
         JFreeChart chart = ChartFactory.createPieChart(
               "Pizza",  // chart title
              dataset,             // data
              true,               // include legend
               true,
              false
          );
   
          PiePlot plot = (PiePlot) chart.getPlot();
          plot.setSectionOutlinesVisible(false);
           plot.setLabelFont(new Font("Verdana", Font.PLAIN, 12));
           plot.setNoDataMessage("No data available");
           plot.setCircular(true);
           plot.setLabelGap(0.02);
          return chart;
          
    }
	 
	 public static JPanel criaPainel(String[] temas, double[]valores) {
         JFreeChart chart = criaChart(PizzaSetDados(temas,valores));
         return new ChartPanel(chart);
     }
	 
	 public static void main(String[] args) {
		 String[] arara = {"arroto","catarro","cuspe"};
  	     double[] valores = {50,70.5,90};
         GraficoPizza2d demo = new GraficoPizza2d("Testando",arara,valores);
         demo.pack();
         RefineryUtilities.centerFrameOnScreen(demo);
         demo.setVisible(true);
    }
}

